package com.hyyt.shopjsp.store.info.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicArea;
import com.hyyt.shopjsp.basic.service.IBasicAreaService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.store.info.pojo.ShopShops;
import com.hyyt.shopjsp.store.info.service.IShopShopsService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/10 0024下午 18:08.
 */

@Controller
@RequestMapping("/back/shops")
public class ShopShopsController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopShopsController.class);
	private String basePath = "/back/shop/shops/";
	@Resource
	private IBasicAreaService areaService;//地区service
	@Resource
	private IShopShopsService shopShopsService;//线下商场service
	@Resource
	private IBasicAreaService basicAreaService;//地区信息service
	/**
	* 进入店铺分类管理
	* @params purviewId
	* @params params
	* @return
	*/
	@RequestMapping("gotoShopsPage")
	public String gotoShopsPage(Map<String, Object> params){
		params.put("parentId", 0);
		params.put("sortColumns", "areaId");
		List<BasicArea> firstAreaList = areaService.queryBasicArea(params);//查询省
		params.put("firstAreaList", firstAreaList);
		return basePath+"shopsList";
	}

	/**
	* 查询所有线下商场
	* @params page
	* @params shopsName			店铺名称
	* @params shopsCode			店铺编号
	* @params createTimeStart	加入开始时间
	* @params createTimeEnd		加入结束时间
	*/
	@RequestMapping(value="findShopsList",method=RequestMethod.POST)
	@ResponseBody
	public void findShopsList(String shopsName,String shopsCode,String createTimeStart,String createTimeEnd,Page page, HttpServletResponse response){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopsName)){
			params.put("shopsName", shopsName.trim());
		}
		if(Utils.objectIsNotEmpty(shopsCode)){
			params.put("shopsCode", shopsCode.trim());
		}
		if(Utils.objectIsNotEmpty(createTimeStart)){
			params.put("startTime", createTimeStart);
		}
		if(Utils.objectIsNotEmpty(createTimeEnd)){
			params.put("endTime", createTimeEnd);
		}
		params.put("sortColumns", "shopsId desc");
		int totalRecordCount =shopShopsService.countShopShops(params);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopShops> shopShopsList =shopShopsService.queryShopShops(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", shopShopsList);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	* 保存或者修改线下商场
	*
	* @params shopShops
	* @return
	*/
	@RequestMapping(value = "saveOrUpdateShops", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateShops(ShopShops shopShops) {
		boolean flag=false;
		if (Utils.objectIsNotEmpty(shopShops)) {
			if (Utils.objectIsNotEmpty(shopShops.getShopsId())) {
				Timestamp date=new Timestamp(shopShops.getCreateTime().getTime());//拿到对象的创建时间
				shopShops.setCreateTime(date);
				Timestamp modifyTime=new Timestamp(System.currentTimeMillis());
				shopShops.setModifyTime(modifyTime);
				shopShopsService.updateShopShops(shopShops);
				flag=true;
			} else {
				//当前时间被格式化后的字符串
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				Timestamp date=new Timestamp(System.currentTimeMillis());//拿到对象的创建时间
				String createTimeStr =sdf.format(date);
				//产生一个[0，1)之间的随机数。
				Double random = Math.random();
				//截取6位随机数
				String num = random.toString().substring(2, 8);
				//当前时间与随机数连接起来得到商城编码
				String shopsCode = createTimeStr+num;
				shopShops.setShopsCode(shopsCode);//设置商城编码
				Timestamp createTime=new Timestamp(System.currentTimeMillis());
				shopShops.setCreateTime(createTime);
				shopShopsService.saveShopShops(shopShops);
				flag=true;
			}
		}
		return ResultMsg.create(flag);
	}

	/**
	* 删除传入的线下商场id
	*
	* @params shopsId
	*/
	@RequestMapping(value = "deleteShops", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg deleteShops(String shopsId) {
		if (Utils.objectIsNotEmpty(shopsId)) {
			shopShopsService.deleteShopShops(Integer.parseInt(shopsId));
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

	/**
	* 根据线下商场id获得对象
	*
	* @params shopsId
	* @params response
	*/
	@RequestMapping(value = "getShopsObject", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg getShopsObject(Integer shopsId) {
		if (Utils.objectIsNotEmpty(shopsId)) {
			Map<String,Object> params = new HashMap<>();
			ShopShops shopShops = shopShopsService.loadShopShops(shopsId);
			BasicArea basicAreaRegionLocation = basicAreaService.loadBasicArea(Integer.parseInt(shopShops.getRegionLocation()));
			BasicArea basicAreaCity = basicAreaService.loadBasicArea(Integer.parseInt(shopShops.getCity()));
			params.put("shopShops",shopShops);
			params.put("basicAreaRegionLocation",basicAreaRegionLocation);
			params.put("basicAreaCity",basicAreaCity);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(params));
		}
		return ResultMsg.create(false);
	}

	/**
	 * 根据areaId 查询下级区域
	 * @param areaId
	 * @return
	 */
	@RequestMapping(value="findAreaList")
	@ResponseBody
	public ResultMsg findAreaList(String areaId){
		Map<String, Object> params=new HashMap<>();
		if(Utils.objectIsNotEmpty(areaId)){
			params.put("parentId", areaId);
			List<BasicArea> areaList = areaService.queryBasicArea(params);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(areaList));
		}
		return ResultMsg.create(false);
	}
	/**
	 * 查询市
	 * @param id
	 * @param response
	 */
	/*@RequestMapping("findCityList")
	public void findCityList(Integer id,HttpServletResponse response){
		Map<String, Object> param=new HashMap<>();
		param.put("parentId", id);
		List<BasicArea> cityList=areaService.queryBasicArea(param);
		String jsonString = FastJsonUtils.toJSONString(cityList);
		param.clear();
		param.put("cityList", jsonString);
		ResultMsg.create(param, response);  //把内容发送到页面
	}*/

}
